package com.caishi.lkx.user.userlog.model;

import cn.hutool.core.map.MapUtil;
import com.alibaba.fastjson.JSON;
import com.caishi.lkx.user.userlog.vo.CountItemClient;
import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

@Getter
@Setter
public class CountItem implements Serializable {

    /**
     * 当天记录数据
     */
    private double todayCount;

    /**
     * 当天记录真实数据
     */
    private double realTodayCount;

    /**
     * 总记录数据
     */
    private double allCount;

    /**
     * 总记录真实数据
     */
    private double realAllCount;

    private Map<String, CountItem> exam;

    public CountItem addTodayCount(double c) {
        this.todayCount += c;
        return this;
    }

    public CountItem addRealTodayCount(double c) {
        this.realTodayCount += c;
        return this;
    }

    public CountItem addAllCount(double c) {
        this.allCount += c;
        return this;
    }

    public CountItem addRealAllCount(double c) {
        this.realAllCount += c;
        return this;
    }

    public CountItem allAll(CountItem data, boolean setExam) {
        return allAll(data, setExam, false);
    }

    public CountItem allAll(CountItem data, boolean setExam, boolean allMax) {
        if (allMax) {
            this.setAllCount(Math.max(data.getAllCount(), this.getAllCount()));
            this.setRealAllCount(Math.max(data.getRealAllCount(), this.getRealAllCount()));
        } else {
            this.addAllCount(data.getAllCount());
            this.addRealAllCount(data.getRealAllCount());
        }
        this.addTodayCount(data.getTodayCount());
        this.addRealTodayCount(data.getRealTodayCount());
        if (setExam) {
            var dataExam = data.getExam();
            var exam = this.getExam();
            if (MapUtil.isNotEmpty(dataExam)) {
                if (exam == null) {
                    exam = new HashMap<>();
                    this.setExam(exam);
                }
                Map<String, CountItem> finalExam = exam;
                dataExam.forEach((k, v) -> {
                    var oldV = finalExam.get(k);
                    if (oldV == null) {
                        oldV = new CountItem();
                        finalExam.put(k, oldV);
                    }
                    oldV.allAll(v, true, allMax);
                });
            }
        }
        return this;
    }


    public CountItemClient toClient() {
        var client = new CountItemClient();
        client.setTodayCount(this.todayCount);
        client.setAllCount(this.allCount);
        return client;
    }

    public CountItem today2All() {
        this.allCount = this.todayCount;
        this.realAllCount = this.realTodayCount;
        return this;
    }


    public Map<String, CountItem> getExam() {
        if (exam == null || exam.isEmpty()) return exam;
        var ok = exam.values().stream().anyMatch(v -> v instanceof CountItem);
        if (ok) return exam;
        var json = JSON.parseObject(JSON.toJSONString(exam));
        var res = new HashMap<String, CountItem>();
        json.forEach((k, v) -> {
            var countObject = json.getJSONObject(k);
            var item = countObject.toJavaObject(CountItem.class);
            res.put(k, item);
        });
        return res;
    }

}
